package com.design.pattern.observe.topic;

import com.design.pattern.observe.observe.Observe;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyingjie
 * @date
 * @remark 天气数据(自实现push的通知方式)
 */
public class WeatherData implements Subject {
    private float temp;
    private float humidity;
    private float pressure;
    private List<Observe> observes;//订阅的观察者

    public WeatherData() {
        this.observes = new ArrayList<>();
    }

    /**
     * 注册观察者(订阅)
     * @param observe
     */
    @Override
    public void registeObserve(Observe observe) {
        observes.add(observe);
    }

    /**
     * 移除观察者(取消订阅)
     * @param observe
     */
    @Override
    public void removeObserve(Observe observe) {
        if (observes.contains(observe)){
            observes.remove(observe);
        }
    }

    /**
     * 通知观察者
     */
    @Override
    public void notifyObserve() {
        for (Observe observe : observes) {
            observe.update(temp,humidity,pressure);
        }
    }

    /**
     * 气象站更新参数时，通知观察者
     */
    public void messurementChanged(){
        notifyObserve();
    }

    /**
     * 设置参数
     * @param temp
     * @param humidity
     * @param pressure
     */
    public void setMessurement(float temp,float humidity,float pressure){
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        messurementChanged();
    }
}
